<?php
namespace app\admin\controller;
use app\admin\model\ForeknowledgeType;
use think\Request;

class Foreknowledge extends Base
{
    protected function _initialize(){
        $this->checkLogin();
        $this->menuLoad();
    }
    public function index(){
        return $this->fetch('public/help');
    }

    public function foreknowledgeList(){
        $foreknowledge = new \app\admin\model\Foreknowledge();
        $where['id']=['neq',0];
        $rs = $foreknowledge->getPageAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function addForeknowledge(Request $request){
        $foreknowledge = new \app\admin\model\Foreknowledge();
        if($request->post()){
            $data = $request->post();
             $rs = $foreknowledge->add($data);
             if($rs){
                 return $this->success('添加成功');
             }else{
                 return $this->error('添加失败');
             }
        }
        $foreknowledgeType = new ForeknowledgeType();
        $where['id'] = ['neq',0];
        $rs = $foreknowledgeType->getAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function editForeknowledge(Request $request){
        $foreknowledge = new \app\admin\model\Foreknowledge();
        if($request->post()){
            $data = $request->post();
            $where['id'] = $data['id'];
            unset($data['id']);
            $rs = $foreknowledge->edit($where,$data);
            if($rs){
                return $this->success('修改成功');
            }
            return $this->error('修改失败');
        }
        $data = $request->get();
        $rs = $foreknowledge->detailJoin($data);

        $this->assign('rs',$rs);

        $foreknowledgeType = new ForeknowledgeType();
        $where['id'] = ['neq',0];
        $type = $foreknowledgeType->getAll($where);
        $this->assign('type',$type);

        return $this->fetch();
    }

}